﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Road3App.Model.CrossroadNs;
using Road3App.Model.RoadNs;
using System.Collections.ObjectModel;
using Road3App.Views.UserControls.EditControls.Crossroad;

namespace Road3App.ViewModels
{
    class AddConnectorViewModel : ViewModelBase
    {
        private CrossRoad _cr;
        private Road _road;

        private CrossroadViewModel _crvm;
        public CrossroadViewModel CrossroadVM
        {
            get { return _crvm; }
            set { _crvm = value; RaisePropertyChanged("CrossroadVM"); }
        }

        private ObservableCollection<Cell> _cellSeq;
        public ObservableCollection<Cell> CellSequence
        {
            get { return _cellSeq; }
            set { _cellSeq = value; RaisePropertyChanged("CellSequence"); }
        }

        private Cell _selectedCell;
        public Cell SelectedCell
        {
            get { return _selectedCell; }
            set { _selectedCell = value; RaisePropertyChanged("SelectedCell"); }
        }

        private Cell _CellStartState;
        public Cell CellStartState
        {
            get { return _CellStartState; }
            set { _CellStartState = value; RaisePropertyChanged("CellStartState"); }
        }

        private Cell _CellFinishState;
        public Cell CellFinishState
        {
            get { return _CellFinishState; }
            set { _CellFinishState = value; RaisePropertyChanged("CellFinishState"); }
        }

        private string _message;
        public string Message
        {
            get { return _message; }
            set { _message = value; RaisePropertyChanged("Message"); }
        }
        
        public AddConnectorViewModel(CrossRoad cr, Road road, string message)
        {
            _cr = cr;
            _road = road;

            Message = message;

            CrossroadVM = new CrossroadViewModel(_cr, "cell");

            CellStartState = new Cell() { State = CellStates.Start };
            CellFinishState = new Cell() { State = CellStates.Finish };

            CellSequence = new ObservableCollection<Cell>();

            CrossroadVM.CellCollection.ToList().ForEach(c => c.MouseLeftButtonDown += c_MouseLeftButtonDown);
        }

        void c_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            CellUC celluc = sender as CellUC;
            if (celluc == null)
                return;
            Cell cell = celluc.DataContext as Cell;
            if (cell == null)
                return;

            CellSequence.Add(cell);
        }

        public void RemoveCellFromSequence()
        {
            if (this.SelectedCell != null)
            {
                CellSequence.Remove(SelectedCell);
            }
        }
    }
}
